const express = require('express');
const router = express.Router();
const Database = require('../model/connectDatabase');
const {SuccessModel,ErrorModel} = require('../model/resData');


router.get('/', function(req, res, next) {
  let database = new Database().mysql;
  let sql = 'select * from (select  resources.id,title,describeStr,username,audioUrl,videoUrl,createTime from resources,users where(users.id=resources.userId)) as atable where(username=? or title=?)'
  let msg = [req.query.keyword,req.query.keyword]
  database.query(sql,msg,(err,data)=>{
    // console.log(data)
    if(err){
      res.send(new ErrorModel(err,'数据库访问错误'))
    }else{
      res.send(new SuccessModel(data,'请求成功'))
    }
  });
});
router.get('/getbyid', function(req, res, next) {
  let database = new Database().mysql;
  let sql = 'select * from resources where(id=?)'
  let msg = [req.query.id]
  database.query(sql,msg,(err,data)=>{
    // console.log(data)
    if(err){
      res.send(new ErrorModel(err,'数据库访问错误'))
    }else{
      res.send(new SuccessModel(data,'请求成功'))
    }
  });
});

module.exports = router;
